package com.crm.web.controller.business;

import com.crm.business.domain.TbBusiness;
import com.crm.business.domain.TbBusinessRecord;
import com.crm.business.domain.dto.TbBursinessDto;
import com.crm.business.domain.vo.TbBusinessVo;
import com.crm.business.service.BusinessService;
import com.crm.common.core.controller.BaseController;
import com.crm.common.core.domain.AjaxResult;
import com.crm.common.core.page.TableDataInfo;
import com.crm.common.domain.vo.AssignMentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/business")
@Slf4j
public class BusinessController extends BaseController {

    @Autowired
    private BusinessService businessService;


    /**
     * 商机分页查询
     *
     * @param tbBusinessVo
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(TbBusinessVo tbBusinessVo) {
        log.info("商机管理分页查询,{}", tbBusinessVo);
        //startPage();
        List<TbBusinessVo> tbBusinessList = businessService.selectBusinessList(tbBusinessVo);
        return getDataTable(tbBusinessList);
    }

    /**
     * 公海池分页查询
     *
     * @param tbBusiness
     * @return
     */
    @GetMapping("pool")
    public TableDataInfo pool(TbBusiness tbBusiness) {
        log.info("商机管理分页查询,{}", tbBusiness);
        //startPage();
        List<TbBusiness> tbBusinessList = businessService.selectBusinessPool(tbBusiness);
        return getDataTable(tbBusinessList);
    }


    /**
     * 商机详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        log.info("商机管理查询,{}", id);
        return AjaxResult.success(businessService.getById(id));
    }


    /**
     * 商机新增
     *
     * @param tbBusiness
     * @return
     */
    @PostMapping
    public AjaxResult add(@RequestBody TbBusiness tbBusiness) {
        log.info("商机管理新增,{}", tbBusiness);
        businessService.add(tbBusiness);
        return AjaxResult.success();
    }

    /**
     * 商机分配
     *
     * @param assignMentVo
     * @return
     */
    @PutMapping("/assignment")
    public AjaxResult assignment(@RequestBody AssignMentVo assignMentVo) {
        log.info("商机管理分配前端传来的参数是,客户id:{},客服id:{}", assignMentVo.getIds(), assignMentVo.getUserId());
        businessService.assignment(assignMentVo);
        return AjaxResult.success();
    }

    /**
     * 商机跟进记录查询
     *
     * @param
     * @return
     */
    @GetMapping("/record/list")
    public AjaxResult recordList(@RequestParam("businessId") Long id) {
        log.info("商机管理记录查询,{}", id);
        List<TbBusinessRecord> tbBusinessRecord = businessService.selectRecordList(id);
        return AjaxResult.success(tbBusinessRecord);
    }

    /**
     * 商机跟进记录新增
     *
     * @param tbBursinessDto
     * @return
     */
    @PostMapping("/record")
    public AjaxResult record(@RequestBody TbBursinessDto tbBursinessDto) {
        log.info("商机管理记录新增详情:,{}", tbBursinessDto);
        return toAjax(businessService.addRecord(tbBursinessDto));
    }

    /**
     * 商机踢回公海池
     *
     * @param
     * @return
     */
    @PutMapping("/back/{id}/{reasons}")
    public AjaxResult back(@PathVariable(value = "id") Long id, @PathVariable(value = "reasons") String reasons) {
        log.info("商机管理回退详情:,{},{}", id, reasons);
        return toAjax(businessService.updateBusinessRecord(id, reasons));
    }

    /**
     * 商机从公海池捞取
     *
     * @param assignMentVo
     * @return
     */
    @PutMapping("/gain")
    public AjaxResult gain(@RequestBody AssignMentVo assignMentVo) {
        log.info("商机管理捞取详情:,{},{}", assignMentVo.getIds(), assignMentVo.getUserId());
        return toAjax(businessService.updateAgin(assignMentVo));
    }





}








